package net.htjs.juc.chapter04;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

@Slf4j
public class ExerciseSell {

    static Random random = new Random();

    /**
     * "D:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.2\lib\idea_rt.jar=49971:D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\IDEAProject\juc-heima\target\classes;D:\repository\org\springframework\boot\spring-boot-starter-web\2.5.6\spring-boot-starter-web-2.5.6.jar;D:\repository\org\springframework\boot\spring-boot-starter\2.5.6\spring-boot-starter-2.5.6.jar;D:\repository\org\springframework\boot\spring-boot\2.5.6\spring-boot-2.5.6.jar;D:\repository\org\springframework\boot\spring-boot-autoconfigure\2.5.6\spring-boot-autoconfigure-2.5.6.jar;D:\repository\org\springframework\boot\spring-boot-starter-logging\2.5.6\spring-boot-starter-logging-2.5.6.jar;D:\repository\ch\qos\logback\logback-classic\1.2.6\logback-classic-1.2.6.jar;D:\repository\ch\qos\logback\logback-core\1.2.6\logback-core-1.2.6.jar;D:\repository\org\apache\logging\log4j\log4j-to-slf4j\2.14.1\log4j-to-slf4j-2.14.1.jar;D:\repository\org\apache\logging\log4j\log4j-api\2.14.1\log4j-api-2.14.1.jar;D:\repository\org\slf4j\jul-to-slf4j\1.7.32\jul-to-slf4j-1.7.32.jar;D:\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\repository\org\yaml\snakeyaml\1.28\snakeyaml-1.28.jar;D:\repository\org\springframework\boot\spring-boot-starter-json\2.5.6\spring-boot-starter-json-2.5.6.jar;D:\repository\com\fasterxml\jackson\core\jackson-databind\2.12.5\jackson-databind-2.12.5.jar;D:\repository\com\fasterxml\jackson\core\jackson-annotations\2.12.5\jackson-annotations-2.12.5.jar;D:\repository\com\fasterxml\jackson\core\jackson-core\2.12.5\jackson-core-2.12.5.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.12.5\jackson-datatype-jdk8-2.12.5.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.12.5\jackson-datatype-jsr310-2.12.5.jar;D:\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.12.5\jackson-module-parameter-names-2.12.5.jar;D:\repository\org\springframework\boot\spring-boot-starter-tomcat\2.5.6\spring-boot-starter-tomcat-2.5.6.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.54\tomcat-embed-core-9.0.54.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.54\tomcat-embed-el-9.0.54.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.54\tomcat-embed-websocket-9.0.54.jar;D:\repository\org\springframework\spring-web\5.3.12\spring-web-5.3.12.jar;D:\repository\org\springframework\spring-beans\5.3.12\spring-beans-5.3.12.jar;D:\repository\org\springframework\spring-webmvc\5.3.12\spring-webmvc-5.3.12.jar;D:\repository\org\springframework\spring-aop\5.3.12\spring-aop-5.3.12.jar;D:\repository\org\springframework\spring-context\5.3.12\spring-context-5.3.12.jar;D:\repository\org\springframework\spring-expression\5.3.12\spring-expression-5.3.12.jar;D:\repository\org\projectlombok\lombok\1.18.20\lombok-1.18.20.jar;D:\repository\org\slf4j\slf4j-api\1.7.32\slf4j-api-1.7.32.jar;D:\repository\org\springframework\spring-core\5.3.12\spring-core-5.3.12.jar;D:\repository\org\springframework\spring-jcl\5.3.12\spring-jcl-5.3.12.jar" net.htjs.juc.chapter4.ExerciseSell
     * 14:47:04.592 [main] INFO net.htjs.juc.chapter4.ExerciseSell - 余票:0
     * 14:47:04.612 [main] INFO net.htjs.juc.chapter4.ExerciseSell - 卖出的票数:10001
     * @param args
     */
    public static void main(String[] args) {
        //模拟多人卖票
        TicketWindow window = new TicketWindow(10000);
        List<Integer> amountList = new Vector<>();
        List<Thread> threadList = new ArrayList<>();
        for(int i = 0; i < 4000; i++) {
            Thread thread = new Thread(() -> {
                int amount = window.sell(randomAmount());
                amountList.add(amount);
            });
            threadList.add(thread);
            thread.start();
        }
        threadList.forEach(thread -> {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        //统计卖出的票数和剩余的票数
        log.info("余票:{}", window.getCount());
        log.info("卖出的票数:{}", amountList.stream().mapToInt(i -> i).sum());
    }

    public static int randomAmount() {
        return random.nextInt(5) + 1;
    }

    static class TicketWindow {
        private int count;

        public TicketWindow(int count) {
            this.count = count;
        }

        public int getCount() {
            return this.count;
        }

        public synchronized int sell(int amount) {
            if(this.count >= amount) {
                this.count -= amount;
                return amount;
            } else {
                return 0;
            }
        }

        /*public int sell(int amount) {
            if(this.count >= amount) {
                this.count -= amount;
                return amount;
            } else {
                return 0;
            }
        }*/
    }
}
